package edu.szu.huhanpeng.doodlingFight.view 
{
	import com.aka.tools.funcTools.CountToCall;
	import com.bit101.components.Panel;
	import com.greensock.TweenLite;
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author Huhanpeng
	 */
	public class StagePanelMed extends Mediator implements IMediator, INotifier 
	{
		public static const NAME:String = "stagePanelMed";
		
		private var _cleanCallback:Function;
		
		
		public function StagePanelMed(panel:Panel) 
		{
			super( NAME, panel );
		}
		
		/////////////////////////////////////////////////////////////////////
		//接口                                                             //
		/////////////////////////////////////////////////////////////////////
		override public function listNotificationInterests():Array 
		{
			return [GlobalConst.noteName.CLEAN_STAGE];
		}
		
		override public function handleNotification(note:INotification):void 
		{
			super.handleNotification(note);
			switch ( note.getName() ) 
			{
				case GlobalConst.noteName.CLEAN_STAGE:
					cleanStage( note.getBody() as Function );
				break;
				default:
			}
		}
		
		public function addChild(child:DisplayObject):DisplayObject
		{
			panel.addChild(child);
			return child;
		}
		
		public function addRawChild(child:DisplayObject):DisplayObject
		{
			panel.addRawChild(child);
			return child;
		}
		
		public function cleanStage( callback:Function = null ):void 
		{
			if ( panel.content.numChildren == 0 ) 
			{
				if ( callback != null ) 
				{
					CountToCall.nextFrameCall( callback );
				}
				return;
			}
			else
			{
				_cleanCallback = callback;
				CountToCall.add( fadeDone, panel.content.numChildren, null, true );
				for (var i:int = 0; i < panel.content.numChildren; i++) 
				{
					fadeOut( panel.content.getChildAt(i) );
				}
			}
		}
		
		public function disableInteract():void 
		{
			panel.mouseChildren = false;
			panel.mouseEnabled = false;
			
		}
		
		public function enableInteract():void 
		{
			panel.enabled = true;
		}
		
		
		
		/////////////////////////////////////////////////////////////////////
		//内部                                                             //
		/////////////////////////////////////////////////////////////////////
		private function fadeDone():void 
		{
			while ( panel.content.numChildren ) 
			{
				panel.content.removeChildAt( 0 );
			}
			
			
			if ( _cleanCallback != null ) 
			{
				CountToCall.nextFrameCall( _cleanCallback );
				_cleanCallback = null;
			}
		}
		
		
		private function fadeOut(child:DisplayObject):void 
		{
			TweenLite.to( child, GlobalConst.setting.FADEOUT, { alpha:0, onComplete:fadeOutComplete } );
		}
		
		private function fadeOutComplete():void 
		{
			CountToCall.count( fadeDone );
		}
		
		
		private function get panel():Panel
		{
			return viewComponent as Panel;
		}
		
	}

}